//
//  NewfeatureViewCell.m
//  LoveFreshBeen
//
//  Created by 辛路琦 on 16/6/11.
//  Copyright © 2016年 xinluqi. All rights reserved.
//

#import "NewfeatureViewCell.h"

@interface NewfeatureViewCell ()

@property (nonatomic, weak) UIImageView *imageView;

@property (nonatomic, weak) UIButton *startButton;

@end

@implementation NewfeatureViewCell

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 1.布局图片
    self.imageView.bounds = self.bounds;
    
    // 2.布局开始按钮
    CGFloat centerX = self.bounds.size.width * 0.5;
    CGFloat centerY = self.bounds.size.height * 0.5 + 250;
    self.startButton.center = CGPointMake(centerX, centerY);
}

- (void)startBtnClick:(UIButton *)btn {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeRootViewController" object:nil];
    
}

#pragma mark - 外部控制方法

- (void)setIndex:(int)index {
    _index = index;
    
    NSString *imageName = [NSString stringWithFormat:@"guide_40_%d", index];
    self.imageView.image = [UIImage imageNamed:imageName];
    
}

- (void)hiddenStartButton:(BOOL)hidden {
    
    // 1.控制按钮的显示和隐藏
    self.startButton.hidden = hidden;
    // 2.判断按钮当前是否是显示的, 如果是就执行动画
    if (!hidden) {
//        self.startButton.transform = CGAffineTransformMakeScale(0.01, 0.01);
    }
}

- (void)startButtonAnim {
    /*
     第一个参数: 动画时长
     第二个参数: 延迟时间
     第三个参数: 震动幅度, 值越大越牛逼
     第四个参数: 初始速度, 0~1
     第五个参数: 动画效果
     第六个参数: 需要执行动画的代码
     第七个参数: 动画执行完毕之后的回调
     */
    [UIView animateWithDuration:0 delay:0.0 usingSpringWithDamping:10 initialSpringVelocity:100 options:kNilOptions animations:^{
        self.startButton.transform = CGAffineTransformIdentity;
    } completion:nil];
    
}

#pragma mark - 懒加载
- (UIImageView *)imageView {
    if (_imageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        // 一定要将子控件添加到contentView中
        [self.contentView addSubview:imageView];
        _imageView =imageView;
    }
    
    return _imageView;
}

- (UIButton *)startButton {
    if (_startButton == nil) {
        UIButton *btn = [[UIButton alloc] init];
        [btn setBackgroundImage:[UIImage imageNamed:@"icon_next"] forState:UIControlStateNormal];
        [btn sizeToFit];
        [btn  addTarget:self action:@selector(startBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btn];
        _startButton = btn;
    }
    
    CGRect frame = self.imageView.frame;
    frame.origin = CGPointMake(0, 0);
    self.imageView.frame = frame;
    
    return _startButton;
}

@end
